package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.enums.OrderStatus;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.order.entity.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author:lyd
 * @Date:2023/9/6 18:31
 * @Version:1.0
 * @Description
 **/

@Slf4j
@Controller
public class PaymentController {

    @Autowired
    OrderFeignClient orderFeignClient;

    /**
     * 支付成功后跳转到这个页面
     * @return
     */
    @GetMapping("/pay/success.html")
    public String success() {

        //支付宝支付成功后会跳到这个页面，并且携带支付宝返回的数据
        //支付完成后关闭浏览器，这个方法不会在被调用
        log.info("支付成功页面跳转.......");
        return "payment/success";
    }

    @GetMapping("/pay.html")
    public String pay(@RequestParam("orderId") Long orderId, Model model) {
        Result<OrderInfo> orderInfo = orderFeignClient.getOrderInfo(orderId);
        OrderInfo data = orderInfo.getData();

        if (data.getOrderStatus().equalsIgnoreCase(OrderStatus.UNPAID.name())) {
            model.addAttribute("orderInfo", data);
        } else {
            model.addAttribute("msg", "本次订单已经支付了！你可以去订单查看");
        }
        return "payment/pay";
    }
}
